package it.happyisland.appgenericaprova;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private Random random = new Random();
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		

		AsyncTask<Void, Void, String> controlliVari = new AsyncTask<Void, Void, String>() {
			
			@Override
			protected String doInBackground(Void... params) {
				try {
					System.setProperty("http.keepAlive", "false");
					URL url = new URL("http://aup-app.appspot.com/stili?obtain");
					URLConnection ucon = url.openConnection();
					ucon.setUseCaches(false); 
					ucon.setConnectTimeout(10000);
					ucon.setDoOutput(true); 
					ucon.setDoInput(true); 
					
					ucon.connect();
					InputStream stream = url.openStream();
					Writer writer = new StringWriter();

		            char[] buffer = new char[1024];
		            try {
		                Reader reader = new BufferedReader(new InputStreamReader(stream));
		                int n;
		                while ((n = reader.read(buffer)) != -1) {
		                    writer.write(buffer, 0, n);
		                }
		            } finally {
		            	stream.close();
		            }
		            return writer.toString();

					
				} catch (Exception e) {
					throw new RuntimeException("ERRORE NEL RECUPERO DEI DATI" + e.getMessage());
				}
			}

			protected void onPostExecute(String stile) {
				createMenu(stile);
				
			}

		};
		
		
		
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			controlliVari.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else {
			controlliVari.execute();
		}
		
		
		
		
		
	}
	private boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	
	private void createMenu(String stile){
		LinearLayout menu = (LinearLayout) findViewById(R.id.menu);
		
		for(int i = 0; i < random.nextInt(4) + 2; i++){
			final TextView button = new TextView(this);
			button.setText("Bottone " + i);
			button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Toast toast = Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG);
					toast.show();
				}
			});
			if(stile.contains("stile1")) {
				button.setTextColor(Color.GREEN);
				button.setBackgroundColor(Color.WHITE);
				
			}
			if(stile.contains("stile2")){
				button.setTextColor(Color.RED);
				button.setBackgroundColor(Color.BLACK);
			}
			//button.setGravity(Gravity.CENTER);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
			button.setLayoutParams(lp);
			button.setTextSize(getResources().getDimension(R.dimen.menu_text_size));
			menu.addView(button);
			
			
			
		}
		
	}
}